package com.example.lowbird.lowbird.adapter;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.lowbird.lowbird.R;
import com.example.lowbird.lowbird.activity.GoodActivity;
import com.example.lowbird.lowbird.bean.HotBean;
import com.example.lowbird.lowbird.fragment.ShopingFragment;
import com.example.lowbird.lowbird.java.SHopcar;
import com.example.lowbird.lowbird.utils.Animatorutils;
import com.example.lowbird.lowbird.utils.Dbutils;
import com.example.lowbird.lowbird.utils.ZhendongUtils;

import java.util.List;

/**
 * Created by Administrator on 2016/11/1.
 */
public class Hotadapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private HotBean hotbean;
    private ObjectAnimator obj;
    private List<HotBean.ListBean> data;

    public Hotadapter(Context mContext, HotBean hotbean) {
        this.mContext = mContext;
        this.hotbean = hotbean;
        inflater = LayoutInflater.from(mContext);


        data = hotbean == null ? null : hotbean.getList();
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        if (convertView == null) {

            convertView = inflater.inflate(R.layout.adapter_hot, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final HotBean.ListBean listBean = data.get(position);

        Glide.with(mContext).load(listBean.getImgUrl()).diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade().into(holder.iv);
        holder.tv1.setText(listBean.getName());
        holder.tv2.setText("¥ " + listBean.getPrice());

        final ViewHolder finalHolder = holder;
        View.OnClickListener listenser=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SHopcar shopcar=new SHopcar(listBean.getId(),listBean.getImgUrl(),listBean.getName(),
                        1,listBean.getPrice(),15,"",1);
                Animatorutils.initanimation(mContext, finalHolder.iv,shopcar);

            }
        };
        holder.tv1.setOnClickListener(listenser);
        holder.iv.setOnClickListener(listenser);

        //TODO 点击事件
        holder.bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("HOT", "onClick: " + listBean.getPrice());

                ZhendongUtils.vSimple(mContext, 20);

                Dbutils.createShop(listBean.getId(), listBean.getImgUrl(), listBean.getName(), 1, listBean.getPrice(), 15, "");


            }
        });
        return convertView;
    }

    public void addHotbean(HotBean bean) {
        if (data == null) {
            data = bean.getList();
        } else {

            data.addAll(bean.getList());
        }
        Log.e("HOT", "addHotbean: " + data.size());
        notifyDataSetChanged();

    }

    public void clearhotbean() {
        data = null;
    }


    class ViewHolder {

        private ImageView iv;
        private TextView tv1;
        private TextView tv2;
        private Button bt;

        public ViewHolder(View view) {

            iv = ((ImageView) view.findViewById(R.id.iv_hotadapter));

            tv1 = ((TextView) view.findViewById(R.id.tv_hotadapter));
            tv2 = ((TextView) view.findViewById(R.id.tv_hotadapter_price));
            bt = ((Button) view.findViewById(R.id.bt_hotbuy));


        }
    }

    private void initanimation(ImageView imageview, final String imgUrl) {


        obj = ObjectAnimator.ofFloat(imageview, "RotationX", -45, 90, -30, 45, 0);

        obj.setDuration(500);

        obj.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mContext.startActivity(new Intent(mContext, GoodActivity.class));
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        obj.start();
    }
}
